package org.practice.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.practice.dao.ShopDaoFactory;
import org.practice.dao.UserDao;
import org.practice.form.VerifyForm;
import org.practice.model.User;

public class RegisterTwoAction extends Action {
	private static Logger logger=Logger.getLogger(RegisterOneAction.class);
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		VerifyForm vf = (VerifyForm)form;
		String verifyCode = vf.getVerifyCode();
		String code = verifyCode.substring(0, verifyCode.lastIndexOf("-"));
		Integer id = Integer.parseInt(verifyCode.substring(verifyCode
				.lastIndexOf("-") + 1));

		UserDao userDao = ShopDaoFactory.createUserDao();

		User user = userDao.verifyUser(id,code);
		if (user == null) {
			// ActionMessages
			ActionMessages errors=new ActionMessages();
			errors.add("error.verify.emailcode",new ActionMessage("error.verify.emailcode"));
			this.saveErrors(request, errors);
			return mapping.findForward("fail");
		}
		return mapping.findForward("success");
	}
	
}
